package com.group.eams.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.AssetDetailQueryDto;
import com.group.eams.pojo.dto.MaintenRecordQueryDto;
import com.group.eams.pojo.entity.*;
import com.group.eams.pojo.entity.MaintenRecord;
import com.group.eams.pojo.entity.MaintenRecord;
import com.group.eams.pojo.vo.FixedAssetListVo;
import com.group.eams.pojo.vo.MaintenRecordListVo;
import com.group.eams.service.MaintenRecordService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 维保记录(MaintenRecord)表控制层
 *
 * @author lqk
 * @since 2024-10-30 12:58:28
 */
@RestController
@RequestMapping("/maintenRecord")
public class MaintenRecordController {
    /**
     * 服务对象
     */
    @Autowired
    private MaintenRecordService maintenRecordService;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "维保记录列表")
    public Result list(MaintenRecordQueryDto maintenRecordQueryDto) {

        //1.准备分页条件
        Page<MaintenRecord> page = new Page<>(maintenRecordQueryDto.getPage(), maintenRecordQueryDto.getLimit());

        //2.设置查询条件
        Page<MaintenRecordListVo> maintenRecordByPage = maintenRecordService.findMaintenRecordByPage(page, maintenRecordQueryDto);

        return Result.buildSuccess(maintenRecordByPage.getTotal(), maintenRecordByPage.getRecords());
    }

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查找维保记录")
    public Result find(@RequestParam Long id) {
        MaintenRecord maintenRecord = maintenRecordService.getById(id);
        return Result.buildSuccess(maintenRecord);
    }

    @RequestMapping(value = "/findByMaintenRecordNum", method = RequestMethod.GET)
    @ApiOperation(value = "根据单号查找资产记录")
    public Result findByMaintenRecordNum(@RequestParam String maintenRecordNum) {
        List<MaintenRecord> maintenRecord = maintenRecordService.findByMaintenRecordNum(maintenRecordNum);
        return Result.buildSuccess(maintenRecord);
    }


    @RequestMapping(value = "/findDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查找资产申请详细")
    public Result findDetail(AssetDetailQueryDto assetDetailQueryDto) {

        //设置查询单号
        assetDetailQueryDto.setMaintenApplyNum(assetDetailQueryDto.getMaintenApplyNum());
        Page<MaintenRecord> page = new Page<>(assetDetailQueryDto.getPage(), assetDetailQueryDto.getLimit());
        Page<FixedAssetListVo> maintenRecordDetailByPage =
                maintenRecordService.findDetailByPage(page, assetDetailQueryDto);
        return Result.buildSuccess(maintenRecordDetailByPage.getTotal(), maintenRecordDetailByPage.getRecords());
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加维保记录")
    public Result add(@RequestBody MaintenRecord maintenRecord) {
        boolean result = maintenRecordService.save(maintenRecord);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改维保记录")
    public Result update(@RequestBody MaintenRecord maintenRecord) {
        boolean result = maintenRecordService.updateById(maintenRecord);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ApiOperation("删除维保记录")
    public Result delete(@RequestParam Long id) {
        boolean result = maintenRecordService.removeById(id);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchRemove", method = RequestMethod.POST)
    @ApiOperation("批量删除维保记录")
    public Result batchDelete(@RequestBody BatchDeleteDto batchDeleteDto) {
        boolean result = maintenRecordService.removeByIds(batchDeleteDto.getIds());
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ApiOperation("查询所有维保记录")
    public Result findAll() {
        List<MaintenRecord> maintenRecordList = maintenRecordService.list();
        return Result.buildSuccess(maintenRecordList);
    }

    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
    @ApiOperation("批量添加保修记录")
    public Result batchAdd(@RequestBody List<MaintenRecord> maintenRecords) {
        boolean result = maintenRecordService.saveBatch(maintenRecords);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchUpdate", method = RequestMethod.POST)
    @ApiOperation("批量修改保修记录")
    public Result batchUpdate(@RequestBody List<MaintenRecord> maintenRecords) {
        boolean result = maintenRecordService.updateBatchById(maintenRecords);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }
}

